package com.jake.xfeed.recyclerfeed;

import android.content.Context;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;

public abstract class BaseCard<Data> extends FrameLayout {
    private int position = -1;//用于兜底
    protected Data data;
    /**
     * 用于保存在RecycleView中的各种。如LayoutPosition、position等
     */
    protected FeedViewHolder viewHolder;

    public BaseCard(@NonNull Context context) {
        super(context);
    }

    /**
     * 由Adapter在创建FeedViewHolder的时候设置
     *
     * @param viewHolder
     */
    public void setViewHolder(FeedViewHolder viewHolder) {
        this.viewHolder = viewHolder;
    }

    public final void bind(int position, Data data) {
        this.position = position;
        this.data = data;
        onBind(position, data);
    }

    public int getPosition() {
        return position;
    }

    public int getAbsoluteAdapterPosition() {
        if (viewHolder != null) {
            return viewHolder.getAbsoluteAdapterPosition();
        }
        return position;
    }

    public Data getData() {
        return data;
    }

    protected abstract void onBind(int position, Data data);

    public void onCardAttachedToWindow() {

    }

    public void onCardDetachedFromWindow() {

    }

    /**
     * 卡片进入回收池
     */
    public void onCardRecycled() {

    }
}